package com.cupk.controller;

import com.cupk.entity.Movie;
import com.cupk.entity.User;
import com.cupk.service.AdminService;
import com.cupk.service.MovieService;
import com.cupk.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author LY
 * @version 1.0
 */
@WebServlet("/Admin")
public class AdminServlet extends HttpServlet {
    AdminService adminService = new AdminService();
    private MovieService movieService = new MovieService();
    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        String IdStr = req.getParameter("Id");
        Integer id = Integer.parseInt(IdStr);
        adminService.deleteMU(id, method);

        HttpSession session = req.getSession();
        if (method.equals("movie")) {
            List<Movie> movieList = movieService.findAll();
            session.setAttribute("movieList", movieList);
            req.getRequestDispatcher("filmAdmin.jsp").forward(req, resp);
        } else {
            List<User> userList = userService.findAll();
            session.setAttribute("userList", userList);
            req.getRequestDispatcher("userAdmin.jsp").forward(req, resp);
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        HttpSession session = req.getSession();
        if(method.equals("addUser")){   //添加新用户
            String username = req.getParameter("title");//用户名
            String password = req.getParameter("password");
            String hobbies = req.getParameter("city");//类型
            userService.register(username,password,hobbies);
            List<User> userList = userService.findAll();
            session.setAttribute("userList", userList);
            req.getRequestDispatcher("userAdmin.jsp").forward(req, resp);
        }else if(method.equals("addMovie")) {
            String name = req.getParameter("name");
            String year = req.getParameter("year");
            year = "(" + year + ")";
            String direct = req.getParameter("direct");
            String actor = req.getParameter("actor");
            String area = req.getParameter("area");
            String type = req.getParameter("type");
            String content = req.getParameter("content");
            String lengthStr = req.getParameter("length");
            Integer length = Integer.parseInt(lengthStr);
            String language = req.getParameter("language");
            movieService.addMovie(name,year,direct,actor,area,type,content,length,language);
            List<Movie> movieList = movieService.findAll();
            session.setAttribute("movieList", movieList);
            req.getRequestDispatcher("filmAdmin.jsp").forward(req, resp);
        }
    }
}
